package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public void add(Dept dept) {
        //补全基础属性
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());

        deptMapper.insert(dept);
    }

    @Override
    public PageResult<Dept> list(String name, Integer status, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);   //我要查询的数据   设置分页参数
        List<Dept> depts = deptMapper.selectlist(name,status);   // 调用mapper接口查询信息   返回list集合
        // 解析结果   强转为Page 最重要获取结果列表   list集合没有提供给总记录数和结果列表  所以要强转
        Page<Dept> p = (Page<Dept>) depts ;
        return new PageResult<>(p.getTotal(), p.getResult());
    }


    @Override
    public Dept getByid(Integer id) {
        return deptMapper.selectByid(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    @Override
    public void delete(Integer id) {
        deptMapper.delete(id);
    }

    @Override
    public List<Dept> all() {
        deptMapper.all();
        return deptMapper.all();
    }
}
